import { generateUUID } from '@/utils'
import { localeProps } from '@/components/FormCreate/src/utils'
import { dateUtil } from '@/utils/dateUtil'
import { useApprovalDesignerStore } from '@/store/modules/dsgn/ApprovalDesigner'

// 定义SerialNumber规则类型配置接口
export interface SnWidgetItemType {
  tagId: string | number
  name: string
  lable: string
  data: any
}

// export default mitt();
export interface SnWidgetDataType {
  label: string
  value: any
}

export const padZero = (num, length) => {
  return String(num).padStart(length, '0')
}


export const getFormTitle=()=>{

  const designerStore = useApprovalDesignerStore()

  let formTitle = ''
  if (designerStore) {
    if (designerStore.designerModel) {
      formTitle = designerStore.designerModel.basic.title
    }
  }
  return formTitle
}

/**
 * 通用选择器规则 hook
 *
 * @param option 规则配置
 */
export const UseSerialNumberRule = (option: any) => {
  const label = option.label
  const name = option.name

  const rules = []

  const formTitle = getFormTitle()


  // 规则编辑器配置
  const propsRule = [
    // {
    //
    //   type: 'input',
    //   field: 'snKey',
    //   title: '规则唯一标识',
    //   info: '规则唯一标识,用于区分不同规则',
    //   value: 'default',
    //   props: {
    //     placeholder: '规则流水号标识'
    //   }
    // },
    {
      type: 'SerialNumberEditor',
      field: 'snEditor',
      info: '编辑编号规则模式',
      title: '编号预览',
      props: {
        tagId: ''
      }
    }
  ]

  return {
    icon: option.icon,
    label,
    name,
    rule() {
      return {
        type: name,
        field: generateUUID(),
        title: label,
        info: '',
        value: null,
        props: {
          snKey: 'default',
          snEditor: [
            {
              tagId: generateUUID(),
              name: 'SnFixedStr',
              label: '固定字符',
              data: {
                label: formTitle,
                value: formTitle
              }
            },
            {
              tagId: generateUUID(),
              name: 'SnDate',
              label: '时间格式',
              data: {
                label: dateUtil().format('YYYYMMDD'),
                value: 'yyyyMMdd'
              }
            },
            {
              data: {
                label: '0001',
                value: {
                  begin: 1,
                  dataType: 1,
                  resetCycle: 6,
                  complementZero: 4
                }
              },
              name: 'SnNumber',
              label: '编号规则',
              tagId: generateUUID()
            }
          ]
        },
        $required: false
      }
    },
    props(_, { t }) {
      if (!option.props) {
        option.props = []
      }
      return localeProps(t, name + '.props', [...option.props, ...propsRule, ...rules])
    }
  }
}
